package com.example.videodemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.jna.HCNetSDKJNAInstance;
import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.INT_PTR;
import com.hikvision.netsdk.NET_DVR_DEVICECFG_V40;

import java.util.ArrayList;
import java.util.List;

import static com.hikvision.netsdk.HCNetSDK.NET_DVR_GET_DEVICECFG_V40;
import static com.hikvision.netsdk.HCNetSDK.NET_DVR_GET_DIGITAL_CHANNEL_STATE;

public class MainActivity extends AppCompatActivity  {
    private DemoViewModel demoViewModel;
    private List<RcvJpg> list;

    private int m_lUserID;
    private DeviceItem deviceItem;
    private RecyclerView recyclerView;
    private TextView textView;
    private SurfaceView m_osurfaceView1;
    private int iPreviewID=-1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        demoViewModel = new ViewModelProvider(this).get(DemoViewModel.class);
        recyclerView = (RecyclerView) findViewById(R.id.rcv_demo);
        textView = (TextView) findViewById(R.id.byDVRType);
        RcvData data=new RcvData("北门", "admin", "Pc@12138", "192.168.7.30", "8000");
        RcvData sfc1 = new RcvData("测试", "admin", "Hik12345", "192.168.7.53", "8000");
        demoViewModel.setRcvData(sfc1);
        m_osurfaceView1=findViewById(R.id.demo_sfv_play);
        m_osurfaceView1.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                m_osurfaceView1.getHolder().setFormat(PixelFormat.TRANSLUCENT);

                if (-1 == iPreviewID) {
                    return;
                }
                Surface surface = holder.getSurface();
                if (surface.isValid()) {
                    if (-1 == demoViewModel.RealPlaySurfaceChanged_jni(iPreviewID, 0, holder))
                        Toast.makeText(getApplicationContext(), "NET_DVR_PlayBackSurfaceChanged" + HCNetSDKJNAInstance.getInstance().NET_DVR_GetLastError(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                if (-1 == iPreviewID) {
                    return;
                }
                if (holder.getSurface().isValid()) {
                    if (-1 == demoViewModel.RealPlaySurfaceChanged_jni(iPreviewID, 0, holder)){
                        Toast.makeText(getApplicationContext(), "NET_DVR_PlayBackSurfaceChanged" + HCNetSDKJNAInstance.getInstance().NET_DVR_GetLastError(), Toast.LENGTH_SHORT).show();

                    }
                }
            }
        });
        m_osurfaceView1.setZOrderOnTop(true);
        deviceItem = demoViewModel.login();
        m_lUserID = deviceItem.m_lUserID;
        init();
    }

    @Override
    protected void onResume() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {




            }
        }, 2000);


        super.onResume();
    }

    private void init() {


        list=new ArrayList<>();
        list=demoViewModel.getRcvChanel();
        int byDVRType = deviceItem.m_struDeviceInfoV40_jna.struDeviceV30.wDevType;
        NET_DVR_DEVICECFG_V40 devicecfg_v40 =new NET_DVR_DEVICECFG_V40();
        Boolean f= HCNetSDK.getInstance().NET_DVR_GetDVRConfig(deviceItem.m_lUserID,NET_DVR_GET_DEVICECFG_V40,0xFFFFFFFF,devicecfg_v40);
       if (f){
           byte[] typeName = devicecfg_v40.byDevTypeName;
           String s=new String(typeName);
           textView.setText("设备类型:"+s);
       }else {
           Log.e("++++",HCNetSDK.getInstance().NET_DVR_GetErrorMsg(new INT_PTR()));

           textView.setText("设备类型:"+byDVRType);
       }


         RcvDemoAdapter adapter = new RcvDemoAdapter(R.layout.rcv_item,list);



        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
               iPreviewID=demoViewModel.startPlay(m_osurfaceView1, iPreviewID,position);
               if(iPreviewID<0){
                   Toast.makeText(getApplicationContext(), "预览失败" + HCNetSDKJNAInstance.getInstance().NET_DVR_GetLastError(), Toast.LENGTH_SHORT).show();

               }
            }
        });

        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
        recyclerView.setAdapter(adapter);

    }


}
